package online.longzipeng.cinemasys.common.interceptor;


import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

/**
 * @author lzp
 * @date 2021/4/25 13:28
 * 访客访问信息拦截
 */
public class IpInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String ip = request.getRemoteAddr();  //访客ip
        String uri = request.getRequestURI(); // 请求地址
        StringBuilder sb = new StringBuilder("IP：");
        sb.append(ip).append(" 请求：").append(uri).append("\n请求参数如下：\n");
        Enumeration<String> parameterNames = request.getParameterNames();
        // 获取请求参数
        while (parameterNames.hasMoreElements()) {
            String key = parameterNames.nextElement();
            String value = request.getParameter(key);
            sb.append(key).append("  :  ").append(value).append("\n");
        }
        System.out.println(sb);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
